//
//  DataBaseUser.m
//  UI豆瓣
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "DataBaseUser.h"
#import "FMDatabase.h"
#import "UserModle.h"


@interface DataBaseUser ()

@property (nonatomic, retain)FMDatabase *db;

@end

@implementation DataBaseUser

+ (instancetype)shareDataBase{
    static DataBaseUser *dbUser = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dbUser = [[DataBaseUser alloc] init];
    });
    return dbUser;
}

- (void)openDB{
    
    if (self.db != nil ) {
        NSLog(@"数据库已经打开");
        return;
    }
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *fileName = [doc stringByAppendingPathComponent:@"user.sqlite"];
    NSLog(@"fileName = %@", fileName);
    
    self.db = [FMDatabase databaseWithPath:fileName];
    
    if ([self.db open]) {
        NSLog(@"数据库打开成功");
    }else {
        NSLog(@"数据库打开失败");
    }
}
//创建表
- (void)createTable{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS user(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, password TEXT, okPassword TEXT, email TEXT, number TEXT)";
    BOOL result = [self.db executeUpdate:sql];
    if (result) {
        NSLog(@"创建表成功");
    }else{
        NSLog(@"创建用户表失败");
    }
}

- (void)insertStudent:(UserModle *)user{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO user(name, password, okPassword, email,number) VALUES('%@', '%@', '%@', '%@', '%@')", user.userName, user.password, user.okPassword, user.email, user.number];
    BOOL result = [self.db executeUpdate:sql];
    if (result) {
        NSLog(@"插入成功");
    }else {
        NSLog(@"插入失败");
    }
}
//注册界面,根据用户注册的用户名查找数据库,如果有信息,就把信息放到数组里
- (NSMutableArray *)selectUser:(NSString *)name{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM user WHERE name = '%@'", name];
    FMResultSet *resultSet = [self.db executeQuery:sql];
    while ([resultSet next]) {
        NSString *nameStr = [resultSet objectForColumnName:@"name"];
        NSString *passw = [resultSet objectForColumnName:@"password"];
        NSString *okPW = [resultSet objectForColumnName:@"okPassword"];
        NSString *e_mail = [resultSet objectForColumnName:@"email"];
        NSString *num = [resultSet objectForColumnName:@"number"];
        UserModle *userM = [[UserModle alloc] init];
        userM.userName = nameStr;
        userM.password = passw;
        userM.okPassword = okPW;
        userM.email = e_mail;
        userM.number = num;
        [array addObject:userM];
    }
    
    return array;
}

//登陆界面,根据登陆的用户名查找用户所有信息,以判断用户名和密码是否相对应
- (UserModle *)selectUserName:(NSString *)name{
    UserModle *userM = [[UserModle alloc] init];
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM user WHERE name = '%@'",name];
    FMResultSet *resultSet = [self.db executeQuery:sql];
    while ([resultSet next]) {
        
        NSString *nameStr = [resultSet objectForColumnName:@"name"];
        NSString *passw = [resultSet objectForColumnName:@"password"];
        
        userM.userName = nameStr;
        userM.password = passw;
    }

    return userM;
}



@end
